package com.five.calendar.database.bean;

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import com.five.calendar.common.database.IDatabaseObject;
import com.five.calendar.database.table.TargetTable;

import java.io.Serializable;
import java.text.SimpleDateFormat;

/**
 * Created by xin on 2017/8/30.
 */

public class TargetBean implements IDatabaseObject, Serializable, Comparable {

    public long id;
    //目标创建时间
    public long createTime;
    //目标标题
    public String title;
    //目标开始时间
    public long startTime;
    //目标结束时间
    public long endTime;
    //目标细节
    public String detail;
    //目标是否完成
    public int isComplete;
    //目标类型
    public String type;
    //目标颜色
    public int color;
    //目标提醒时间
    public String reminderText;

    public TargetBean(){}

    public TargetBean(String title, long startTime, long endTime, String detail, String type, int color, String reminderText, int isComplete){
        this.title = title;
        this.startTime = startTime;
        this.endTime = endTime;
        this.detail = detail;
        this.type = type;
        this.color = color;
        this.reminderText = reminderText;
        this.isComplete = isComplete;
    }
    @Override
    public void writeObject(ContentValues values, String table) {
        if(TargetTable.TABLE_NAME.equals(table)){
            values.put(TargetTable.CREATETIME, createTime);
            values.put(TargetTable.TITLE, title);
            values.put(TargetTable.STARTTIME, startTime);
            values.put(TargetTable.ENDTIME, endTime);
            values.put(TargetTable.DETAIL, detail);
            values.put(TargetTable.TYPE, type);
            values.put(TargetTable.COLOR, color);
            values.put(TargetTable.REMINDERTEXT, reminderText);
            values.put(TargetTable.ISCOMPLETE, isComplete);
        }
    }

    @Override
    public void readObject(Cursor cursor, String table) {
        if(TargetTable.TABLE_NAME.equals(table)){
            id = cursor.getLong(cursor.getColumnIndex(TargetTable.ID));
            createTime = cursor.getLong(cursor.getColumnIndex(TargetTable.CREATETIME));
            title = cursor.getString(cursor.getColumnIndex(TargetTable.TITLE));
            startTime = cursor.getLong(cursor.getColumnIndex(TargetTable.STARTTIME));
            endTime = cursor.getLong(cursor.getColumnIndex(TargetTable.ENDTIME));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Log.e("ZXX", "startTime: " + sdf.format(startTime));
            Log.e("ZXX", "endTime: " + sdf.format(endTime));
            detail = cursor.getString(cursor.getColumnIndex(TargetTable.DETAIL));
            type = cursor.getString(cursor.getColumnIndex(TargetTable.TYPE));
            color = cursor.getInt(cursor.getColumnIndex(TargetTable.COLOR));
            reminderText = cursor.getString(cursor.getColumnIndex(TargetTable.REMINDERTEXT));
            isComplete = cursor.getInt(cursor.getColumnIndex(TargetTable.ISCOMPLETE));
        }
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof TargetBean){
            TargetBean targetBean = (TargetBean) o;
            long now = System.currentTimeMillis();
            if (endTime < now && isComplete == 0){
                return 1;
            } else if (targetBean.endTime < now && targetBean.isComplete == 0){
                return -1;
            } else if (isComplete > targetBean.isComplete){
                return 1;
            } else if (isComplete < targetBean.isComplete){
                return -1;
            }
        }
        return 0;
    }
}
